#include "textkvdialog.h"
#include "ui_textkvdialog.h"

TextKVDialog::TextKVDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TextKVDialog)
{
    ui->setupUi(this);

    connect(ui->plan_keys, &QPlainTextEdit::textChanged, this, &TextKVDialog::slot_plan_textChanged);
    connect(ui->plan_values, &QPlainTextEdit::textChanged, this, &TextKVDialog::slot_plan_textChanged);
}

TextKVDialog::~TextKVDialog()
{
    delete ui;
}

QMap<QString, QString> TextKVDialog::keyMap() const
{
    QMap<QString, QString> kv;

    auto keys = ui->plan_keys->toPlainText().split("\n", Qt::SkipEmptyParts);
    auto values = ui->plan_values->toPlainText().split("\n", Qt::SkipEmptyParts);

    for (int var = 0; var < keys.length(); ++var) {
        auto key = keys[var];
        auto val = values[var];

        kv.insert(key, val);
    }

    return kv;
}

void TextKVDialog::slot_plan_textChanged()
{
    int keys_lines = ui->plan_keys->toPlainText().split("\n", Qt::SkipEmptyParts).length();
    int values_lines = ui->plan_values->toPlainText().split("\n", Qt::SkipEmptyParts).length();

    ui->buttonBox->setEnabled(keys_lines == values_lines);
}
